﻿


using AutoMapper;
using B.S.Business.Domain;
using B.S.Business.ErrorCode;
using B.S.Business.Infacetrue;
using B.S.Business.Write.API.Applications.Command;
using MediatR;

namespace B.S.Business.Write.API.Applications.CommandHandler
{

    public class PurchaseDelCommandHandler : IRequestHandler<PurchaseDelCommand, ApiResult<object>>
    {
        private readonly IBaseRepository<Purchase> purchaseRepo;
        private readonly IMapper mapper;

        public PurchaseDelCommandHandler(IBaseRepository<Purchase> purchaseRepo, IMapper mapper)
        {
            this.purchaseRepo = purchaseRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 采购删除处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<object>> Handle(PurchaseDelCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();

            var obj = purchaseRepo.GetAll().FirstOrDefault(x => x.PurchaseId == request.PurchaseId);
            obj.IsDeleted = true;
            var res = purchaseRepo.Update(obj);
            if (res == 1)
            {
                result.Code = ApiCode.删除成功;
                result.Msg = ApiCode.删除成功.ToString();
                result.Data = res;
            }
            else
            {
                result.Code = ApiCode.删除失败;
                result.Msg = ApiCode.删除失败.ToString();
                result.Data = res;
            }
            return Task.FromResult(result);
        }
    }
}
